﻿using MixingStation.LTWebService;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MixingStation
{
    public partial class WeightRecordAddForm : Form
    {
        private string m_CurrentUserName = "";
        //选择的订单
        private purchaseOrderWS m_SelectPurchaseOrder = null;
        //选择的物料编码
        private string m_MaterialCode = "";
        //选择的供应商编码
        private string m_SupplierCode = "";
        //选择的供应商编码
        private string m_PlaceCode = "";
        //选择的流向编码
        private string m_DirectionCode = "";

        private List<materialWS> m_MaterialList = new List<materialWS>();
        public List<materialWS> MaterialList
        {
            set { m_MaterialList = value; }
        }
        private List<supplierWS> m_SupplierList = new List<supplierWS>();
        public List<supplierWS> SupplierList
        {
            set { m_SupplierList = value; }
        }
        private List<placeWS> m_PlaceList = new List<placeWS>();
        public List<placeWS> PlaceList
        {
            set { m_PlaceList = value; }
        }
        private List<DirectionInfo> m_DirectionList = new List<DirectionInfo>();
        public List<DirectionInfo> DirectionList
        {
            set { m_DirectionList = value; }
        }

        private WeightRecordInfo m_CurrentRecordInfo = null;
        public WeightRecordInfo CurrentRecordInfo
        {
            get { return m_CurrentRecordInfo; }
        }

        public WeightRecordAddForm()
        {
            InitializeComponent();
        }

        private void WeightRecordAddForm_Load(object sender, EventArgs e)
        {
            InitCmbMaterial();
            InitCmbSupplier();
            InitCmbPlace();
            InitCmbDirection();
            this.firstTime.EditValue = DateTime.Now;
            this.lastTime.EditValue = DateTime.Now;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            string carNumber = txtCarCode.Text.Trim();
            if (string.IsNullOrEmpty(carNumber))
            {
                MessageBox.Show("请输入车牌号!");
                txtCarCode.Focus();
                return;
            }
            if (!CommonHelper.InValidData(carNumber))
            {
                MessageBox.Show("请输入正确格式车牌号!");
                txtCarCode.Focus();
                return;
            }
            if (carNumber.Length == 8)
                carNumber += "..";
            else if (carNumber.Length == 9)
                carNumber += ".";
            decimal grossWeight = decimal.Parse(txtGrossWeight.Text.Trim());
            decimal tareWeight = decimal.Parse(txtTareWeight.Text.Trim());
            decimal buckleWeight = decimal.Parse(txtBuckleWeight.Text.Trim());
            decimal netWeight = grossWeight - tareWeight - buckleWeight;
            if (tareWeight <= 0)
            {
                MessageBox.Show("数据补录需输入皮重!");
                txtTareWeight.Focus();
                return;
            }
            if (netWeight <= 0)
            {
                MessageBox.Show("毛重应该大于等于皮重与扣杂之和!");
                txtGrossWeight.Focus();
                return;
            }
            m_CurrentRecordInfo = new WeightRecordInfo();
            if (rdgType.SelectedIndex == 0)
            {
                if (string.IsNullOrEmpty(btnOrderSelect.Text.Trim()))
                {
                    MessageBox.Show("采购物资数据补录时需选择采购订单！");
                    return;
                }
                m_CurrentRecordInfo.PurchaseOrder = m_SelectPurchaseOrder;
            }
            else
            {
                m_CurrentRecordInfo.MaterialCode = m_MaterialCode;
                m_CurrentRecordInfo.MaterialName = this.cmbMaterial.Text.Trim();
                m_CurrentRecordInfo.SupplierCode = m_SupplierCode;
                m_CurrentRecordInfo.SupplierName = this.cmbSupplier.Text.Trim();
                m_CurrentRecordInfo.PlaceCode = m_PlaceCode;
                m_CurrentRecordInfo.PlaceName = this.cmbPlace.Text.Trim();
            }
            m_CurrentRecordInfo.DirectionCode = m_DirectionCode;
            m_CurrentRecordInfo.DirectionName = this.cmbDirection.Text.Trim();
            m_CurrentRecordInfo.CarCode = carNumber;
            m_CurrentRecordInfo.GrossWeight = grossWeight;
            m_CurrentRecordInfo.TareWeight = tareWeight;
            m_CurrentRecordInfo.BuckleWeight = buckleWeight;
            m_CurrentRecordInfo.BuckleReasons = this.cmbBuckleReasons.Text.Trim();
            m_CurrentRecordInfo.NetWeight = netWeight;
            m_CurrentRecordInfo.Remark = txtRemark.Text.Trim();

            m_CurrentRecordInfo.FirstWeightTime = (DateTime)this.firstTime.EditValue;
            m_CurrentRecordInfo.LastWeightTime = (DateTime)this.lastTime.EditValue;

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        //选择订单
        private void btnOrderSelect_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            PurchaseOrderSelectForm form = new PurchaseOrderSelectForm();
            form.MaterialArr = m_MaterialList.ToArray();
            form.SupplierArr = m_SupplierList.ToArray();
            form.MaterialCode = m_MaterialCode;
            form.SupplierCode = m_SupplierCode;
            if (form.ShowDialog() != DialogResult.OK)
                return;
            m_SelectPurchaseOrder = form.SelectPurchaseOrder;
            if (m_SelectPurchaseOrder == null)
                return;
            btnOrderSelect.Text = m_SelectPurchaseOrder.code;
            if (m_SelectPurchaseOrder.material != null)
                cmbMaterial.SelectedItem = CommonHelper.FindDataInfoByKey(cmbMaterial, m_SelectPurchaseOrder.material.code);
            if (m_SelectPurchaseOrder.supplier != null)
                cmbSupplier.SelectedItem = CommonHelper.FindDataInfoByKey(cmbSupplier, m_SelectPurchaseOrder.supplier.code);
            if (m_SelectPurchaseOrder.place != null)
                cmbPlace.SelectedItem = CommonHelper.FindDataInfoByKey(cmbPlace, m_SelectPurchaseOrder.place.code);
        }

        #region 初始化
        //初始化物料下拉框
        private void InitCmbMaterial()
        {
            try
            {
                if (m_MaterialList == null || m_MaterialList.Count == 0)
                    return;
                var list = new List<DataInfo>();
                list.Add(new DataInfo { Id = "", Value = "" });
                foreach (materialWS material in m_MaterialList)
                    list.Add(new DataInfo { Id = material.code, Value = material.name });
                cmbMaterial.Properties.Items.Clear();
                foreach (DataInfo item in list)
                    cmbMaterial.Properties.Items.Add(item);
            }
            catch (Exception ex)
            {
                LogHelper.error("初始化物料下拉框异常：" + ex.ToString());
            }
        }

        //初始化供应商下拉框
        private void InitCmbSupplier()
        {
            try
            {
                if (m_SupplierList == null || m_SupplierList.Count == 0)
                    return;
                var list = new List<DataInfo>();
                list.Add(new DataInfo { Id = "", Value = "" });
                foreach (supplierWS supplier in m_SupplierList)
                    list.Add(new DataInfo { Id = supplier.code, Value = supplier.name });
                cmbSupplier.Properties.Items.Clear();
                foreach (DataInfo item in list)
                    cmbSupplier.Properties.Items.Add(item);
            }
            catch (Exception ex)
            {
                LogHelper.error("初始化供应商下拉框异常：" + ex.ToString());
            }
        }

        //加载产地信息
        private void InitCmbPlace()
        {
            try
            {
                if (m_PlaceList == null || m_PlaceList.Count == 0)
                    return;
                var list = new List<DataInfo>();
                list.Add(new DataInfo { Id = "", Value = "" });
                foreach (placeWS place in m_PlaceList)
                    list.Add(new DataInfo { Id = place.code, Value = place.name });
                cmbPlace.Properties.Items.Clear();
                foreach (DataInfo item in list)
                    cmbPlace.Properties.Items.Add(item);
            }
            catch (Exception ex)
            {
                LogHelper.error("加载产地下拉框异常：" + ex.Message);
            }
        }

        private void InitCmbDirection()
        {
            try
            {
                string defaultDirectionCode = "";
                if (m_DirectionList == null || m_DirectionList.Count == 0)
                    return;
                var list = new List<DataInfo>();
                list.Add(new DataInfo { Id = "", Value = "" });
                foreach (DirectionInfo direction in m_DirectionList)
                {
                    if (direction.IsDefault)
                        defaultDirectionCode = direction.Code;
                    list.Add(new DataInfo { Id = direction.Code, Value = direction.Name });
                }
                cmbDirection.Properties.Items.Clear();
                foreach (DataInfo item in list)
                    cmbDirection.Properties.Items.Add(item);
                cmbDirection.SelectedItem = CommonHelper.FindDataInfoByKey(cmbDirection, defaultDirectionCode);
            }
            catch (Exception ex)
            {
                LogHelper.error("加载产地下拉框异常：" + ex.Message);
            }
        }
        #endregion

        #region 下拉框事件
        //选择的物料
        private void cmbMaterial_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbMaterial.SelectedItem == null)
                m_MaterialCode = "";
            else
                m_MaterialCode = ((DataInfo)cmbMaterial.SelectedItem).Id;
        }

        //选择的供应商
        private void cmbSupplier_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbSupplier.SelectedItem == null)
                m_SupplierCode = "";
            else
                m_SupplierCode = ((DataInfo)cmbSupplier.SelectedItem).Id;
        }

        //选择的产地
        private void cmbPlace_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbPlace.SelectedItem == null)
                m_PlaceCode = "";
            else
                m_PlaceCode = ((DataInfo)cmbPlace.SelectedItem).Id;
        }

        private void cmbDirection_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbDirection.SelectedItem == null)
                m_DirectionCode = "";
            else
                m_DirectionCode = ((DataInfo)cmbDirection.SelectedItem).Id;
        }
        #endregion

        private void txtWeight_TextChanged(object sender, EventArgs e)
        {
            decimal grossWeight = decimal.Parse(txtGrossWeight.Text.Trim());
            decimal tareWeight = decimal.Parse(txtTareWeight.Text.Trim());
            decimal buckleWeight = decimal.Parse(txtBuckleWeight.Text.Trim());
            decimal netWeight = grossWeight - tareWeight - buckleWeight;
            if (netWeight > 0)
                this.txtNetWeight.Text = netWeight.ToString();
            else
                this.txtNetWeight.Text = "0";
        }


    }
}
